layui.use(['jquery', 'common', 'table', 'form', 'layer'], function() {
	var $ = layui.jquery;
	var common = layui.common;
	var table = layui.table;
	var form = layui.form;
	var layer = layui.layer;

	table.render({
		elem: '#table',
		url: 'showTableData',
		page: true,
		skin: 'line',
		request: {
			pageName: 'current',
			limitName: 'size'
		},
		cellMinWidth: 80,
		cols: [[
			{field: 'gameId', title: '编号', rowspan: 2, sort: true, templet: '<div>No.{{d.gameId}}</div>'},
			{field: 'avatar', title: '头像', rowspan: 2, templet: common.tableImageTemplet('avatar', 65, 'name')},
			{field: 'name', title: '姓名', rowspan: 2, minWidth: 250},
			{title: '职介', rowspan: 2, templet: function(d) {
				var image = '';
				switch (d.star) {
				case 0:
					image = d.clazz.blackIcon;
					break;
				case 1:
				case 2:
					image = d.clazz.bronzeIcon;
					break;
				case 3:
					image = d.clazz.silverIcon;
					break;
				case 4:
				case 5:
					image = d.clazz.goldenIcon;
					break;
				}
				return '<img class="table-image" src="' + common.getImageUrl(image, 40) + '" layer-src="' + common.getImageUrl(image) + '" alt="' + d.clazz.name + '">';
			}},
			{field: 'star', title: '星级', rowspan: 2, sort: true, templet: '<div><i class="layui-icon layui-icon-rate-solid"></i>{{d.star}}</div>'},
			{field: 'cost', title: 'cost', rowspan: 2, sort: true},
			{title: '宝具', align: 'center', rowspan: 2, templet: function(d) {
				return d.npColorCard ? '<div class="np"><img src="' + common.ctxPath + '/img/' + d.npColorCard + '.png"><p>' + d.npType + '</p></div>' : '';
			}},
			{title: '指令卡', rowspan: 2, width: 175, templet: function(d) {
				if (d.cards) {
					var str = '<div class="cards">';
					$.each(d.cards, function() {
						str += '<img src="' + common.ctxPath + '/img/' + this + '.png">';
					});
					str += '</div>';
					return str;
				} else {
					return '';
				}
			}},
			{title: 'HP', colspan: 3},
			{title: 'ATK', colspan: 3},
			{field: 'height', title: '身高', rowspan: 2, sort: true},
			{field: 'weight', title: '体重', rowspan: 2, sort: true},
			{title: '数值', colspan: 6},
			{title: 'np率', colspan: 6},
			{title: 'Hit', colspan: 5},
			{field: 'starRate', title: '出星率', rowspan: 2, minWidth: 95, sort: true, templet: function(d) {
				return typeof d.starRate == 'number' ? d.starRate + '%' : '';
			}},
			{field: 'beKilled', title: '即死补正', rowspan: 2, minWidth: 110, sort: true, templet: function(d) {
				return typeof d.beKilled == 'number' ? d.beKilled + '%' : '';
			}},
			{field: 'collectingStars', title: '集星权重', minWidth: 110, rowspan: 2, sort: true},
		], [
			{field: 'initialHp', title: '初始', sort: true},
			{field: 'fullLevelHp', title: '满级', sort: true},
			{field: 'hundredHp', title: '百级', sort: true},
			{field: 'initialAtk', title: '初始', sort: true},
			{field: 'fullLevelAtk', title: '满级', sort: true},
			{field: 'hundredAtk', title: '百级', sort: true},
			{field: 'strength', title: '筋力', sort: true},
			{field: 'endurance', title: '耐久', sort: true},
			{field: 'agility', title: '敏捷', sort: true},
			{field: 'mana', title: '魔力', sort: true},
			{field: 'luck', title: '幸运', sort: true},
			{field: 'npLevel', title: '宝具', sort: true},
			{field: 'npQuick', title: 'Quick', minWidth: 95, sort: true, templet: function(d) {
				return typeof d.npQuick == 'number' ? d.npQuick + '%' : '';
			}},
			{field: 'npArts', title: 'Arts', minWidth: 95, sort: true, templet: function(d) {
				return typeof d.npArts == 'number' ? d.npArts + '%' : '';
			}},
			{field: 'npBuster', title: 'Buster', minWidth: 95, sort: true, templet: function(d) {
				return typeof d.npBuster == 'number' ? d.npBuster + '%' : '';
			}},
			{field: 'npExtra', title: 'Extra', minWidth: 95, sort: true, templet: function(d) {
				return typeof d.npExtra == 'number' ? d.npExtra + '%' : '';
			}},
			{field: 'npNp', title: '宝具', minWidth: 95, sort: true, templet: function(d) {
				return typeof d.npNp == 'number' ? d.npNp + '%' : '';
			}},
			{field: 'npSuffer', title: '受击', minWidth: 95, sort: true, templet: function(d) {
				return typeof d.npSuffer == 'number' ? d.npSuffer + '%' : '';
			}},
			{field: 'hitQuick', title: 'Quick', minWidth: 95, sort: true, templet: function(d) {
				if (typeof d.hitQuick == 'number') {
					if (d.hitQuick == 1) {
						return d.hitQuick + 'Hit';
					} else {
						return d.hitQuick + 'Hits';
					}
				} else {
					return '';
				}
			}},
			{field: 'hitArts', title: 'Arts', minWidth: 95, sort: true, templet: function(d) {
				if (typeof d.hitArts == 'number') {
					if (d.hitArts == 1) {
						return d.hitArts + 'Hit';
					} else {
						return d.hitArts + 'Hits';
					}
				} else {
					return '';
				}
			}},
			{field: 'hitBuster', title: 'Buster', minWidth: 95, sort: true, templet: function(d) {
				if (typeof d.hitBuster == 'number') {
					if (d.hitBuster == 1) {
						return d.hitBuster + 'Hit';
					} else {
						return d.hitBuster + 'Hits';
					}
				} else {
					return '';
				}
			}},
			{field: 'hitExtra', title: 'Extra', minWidth: 95, sort: true, templet: function(d) {
				if (typeof d.hitExtra == 'number') {
					if (d.hitExtra == 1) {
						return d.hitExtra + 'Hit';
					} else {
						return d.hitExtra + 'Hits';
					}
				} else {
					return '';
				}
			}},
			{field: 'hitNp', title: '宝具', minWidth: 95, sort: true, templet: function(d) {
				if (typeof d.hitNp == 'number') {
					if (d.hitNp == 1) {
						return d.hitNp + 'Hit';
					} else {
						return d.hitNp + 'Hits';
					}
				} else {
					return '';
				}
			}},
		]],
		autoSort: false,
		initSort: {
			field: 'gameId',
			type: 'desc'
		},
		where: {
			orderByField: 'gameId',
			isAsc: false
		},
		done: function(res, curr, count) {
			common.tableImagePhotos();
			// 提示框
			var heightTips = null;
			$('th[data-field="height"]').hover(function() {
				heightTips = layer.tips('排序根据数值排序，不进行单位转换，仅供参考', this);
			}, function() {
				if (heightTips) {
					layer.close(heightTips);
				}
			});
			var weightTips = null;
			$('th[data-field="weight"]').hover(function() {
				weightTips = layer.tips('排序根据数值排序，不进行单位转换，仅供参考', this);
			}, function() {
				if (weightTips) {
					layer.close(weightTips);
				}
			});
		}
	});
	table.on('sort', function(obj) {
		table.reload('table', {
			initSort: obj,
			where: {
				orderByField: obj.field,
				isAsc: obj.type ? obj.type == 'asc' : null
			}
		})
	});
	form.on('submit(search)', function(data) {
		table.reload('table', {
			where: data.field,
			page: {
				curr: 1
			}
		});
	});
});